
ENABLE_DEBUG = false

--------------------------------------------------------------------------------------
-- Color Constants -------------------------------------------------------------------
--------------------------------------------------------------------------------------

COLOR_WHITE = "|CFFFFFFFF"
COLOR_LIGHTBLUE = "|CFF99CCFF"
COLOR_BRIGHTGREEN = "|CFF00FF00"
COLOR_LIGHTGREEN2 = "|CFF66FF66"
COLOR_LIGHTGREEN1 = "|CFF99FF66"
COLOR_YELLOWGREEN = "|CFFCCFF66"
COLOR_LIGHTYELLOW = "|CFFFFFF66"
COLOR_RED = "|CFFFF0000"
COLOR_LIGHTRED = "|CFFFF5555"
COLOR_LIGHTPURPLE1 = "|CFFFFC4FF"
COLOR_LIGHTPURPLE2 = "|CFFFF99FF"
COLOR_PURPLE = "|CFFFF50FF"
COLOR_DARKPURPLE1 = "|CFFFF00FF"
COLOR_DARKPURPLE2 = "|CFFB700B7"
COLOR_DARKYELLOW = "|CFFFFCC00"
COLOR_LIGHTORANGE = "|CFFFFCC66"
COLOR_DIRTYORANGE = "|CFFFF9933"
COLOR_DARKORANGE = "|CFFFF6600"
COLOR_REDORANGE = "|CFFFF3300"
COLOR_GREY = "|CFFC0C0C0"

COLOR_NORMAL = "|CFFEBEBFF"
COLOR_ERROR = "|CFF4040FF"
COLOR_HEADER = "|CFF8080FF"
COLOR_VALUE = "|CFF8080FF"
COLOR_NOTIFY = "|CFFC0C0FF"
COLOR_TITLE = "|CFF2065C2"
COLOR_BRACKET = "|CFF01059D"


function ValueByName(tbl, nme)
    local i = next(tbl)
    while i~=nil do
        if tbl[i].Name == nme then
            return tbl[i];
        end
        i = next(tbl, i)
    end
    return nil
end
function IndexByName(tbl, name)
    
    local i = next(tbl)
    while i~=nil do
        if tbl[i].Name==name then
            return i
        end
        i = next(tbl, i)
    end
    return nil
end
function TableCount(tbl)
    if tbl==nil then
        return 0
    else
        return table.getn(tbl)
    end
end
function ClearTable(tbl)
    i = next(tbl)
    while i~=nil do
        table.remove(tbl, i)
        i = next(tbl)
    end
end

--------------------------------------------------------------------------------------
-- Message Functions -----------------------------------------------------------------
--------------------------------------------------------------------------------------
function ScreenMessage(txt)
    UIErrorsFrame:AddMessage(txt, 0.9, 0.7, 0.0, 1.0, 3);
end

function EchoMessage(prefix, txt, msg)
	DEFAULT_CHAT_FRAME:AddMessage(COLOR_BRACKET.."["..COLOR_TITLE..prefix..COLOR_BRACKET.."] "..COLOR_NOTIFY..txt.." "..COLOR_BRACKET.."["..COLOR_TITLE..msg..COLOR_BRACKET.."]",1,1,0)
end


function EchoColor(prefix, color, txt)
	DEFAULT_CHAT_FRAME:AddMessage(COLOR_BRACKET.."["..COLOR_TITLE..prefix..COLOR_BRACKET.."] "..color..txt,1,1,0)
end

function Echo(txt)
    if ENABLE_DEBUG then
        DEFAULT_CHAT_FRAME:AddMessage(COLOR_BRACKET.."["..COLOR_TITLE.."Debug"..COLOR_BRACKET.."] "..COLOR_NORMAL..txt.." "..COLOR_BRACKET.."["..COLOR_TITLE.."Debug"..COLOR_BRACKET.."]",1,1,0)
    end
end


function EchoEvent()
	if arg1==nil then arg1="nil" end
	if arg2==nil then arg2="nil" end
	if arg3==nil then arg3="nil" end
	if arg4==nil then arg4="nil" end
	if arg5==nil then arg5="nil" end
	if arg6==nil then arg6="nil" end
	if arg7==nil then arg7="nil" end
	if arg8==nil then arg8="nil" end
	if arg9==nil then arg9="nil" end
	if event==nil then event="nil" end
	Echo(event.."("..tostring(arg1)..", "..tostring(arg2)..", "..tostring(arg3)..", "..tostring(arg4)..", "..tostring(arg5)..", "..tostring(arg6)..", "..tostring(rg7)..", "..tostring(arg8)..", "..tostring(arg9)..")")
end
function Speak(txt)
	if GetNumRaidMembers()>0 then
		SendChatMessage("<<"..txt..">>","RAID")
	elseif GetNumPartyMembers()>0 then
		SendChatMessage("<<"..txt..">>","PARTY")
	else
		--SendChatMessage("<<"..txt..">>","SAY")
	end
end
function WhisperName(name, msg)
	if (name ~= UnitName("player")) then
		SendChatMessage("<<"..msg..">>", "WHISPER", nil, name)
	end	
end

function DebugButton(btn)
    local typ = btn:GetAttribute("type")
    if typ~=nil then
    Echo("Type: "..typ)
    if typ=="spell" then
        Echo("Spell: "..btn:GetAttribute("spell"))
    elseif typ=="macro" then
        Echo("Macro: "..btn:GetAttribute("macrotext"))
    end
    else
    Echo("Type: nil")
    end
end

--------------------------------------------------------------------------------------
-- Various Functions -----------------------------------------------------------------
--------------------------------------------------------------------------------------

--itemBagNum=-1
--itemSlotNum=0
function GetContainerItemByName(item)
	--itemBagNum=-1; itemSlotNum=0;
	for i=0,4 do
		for c=1,GetContainerNumSlots(i) do
			y=GetContainerItemLink(i,c)
            
			if (y~=nil) then

                if string.find(y,item) then
                    --itemBagNum=i;itemSlotNum=c;
                    return i, c;
                end
			end
		end
	end
	return nil;
end
function FindContainerItemByName(item)
	for i=0,4 do
		for c=1,GetContainerNumSlots(i) do
			y=GetContainerItemLink(i,c)
			if (y~=nil) then
                if string.find(y,item) then
                    return { i, c };
                end
			end
		end
	end
	return nil;
end

function ReturnSetting(value, default)
	return iif(((value==nil) or (value=="")),default,value)
end

function IsInCombat(unit)
	return UnitAffectingCombat(unit)
end

function IsUnitBuff(unit, texture)
	for i=1, 40 do
		local a,b,c = UnitBuff(unit, i)
		if c~=nil then
            if (string.find(c, texture)) then
                return true
            end
		end
	end
	return false
end

function DebugUnitBuffs(unit)
	local i= 1
	for i=1, 40 do
		local a,b,c = UnitBuff(unit, i)
		if c~=nil then
			Echo("Buff: "..c)
		end
	end
	return false
end

function IsUnitDebuff(unit, texture) 
	local i= 1
	for i=1, 40 do
		local a,b,c = UnitDebuff(unit, i)
		if c~=nil then
	    		if (string.find(c, texture)) then
      				return true
    			end
		end
	end
	return false
end
function DebugUnitBuffs(unit)
	local i= 1
	for i=1, 40 do
		local a,b,c = UnitDebuff(unit, i)
		if c~=nil then
			Echo("Debuff: "..c)
		end
	end
	return false
end

function iif(exp,rtrue,rfalse)
	if exp then
		return rtrue
	else
		return rfalse
	end
end
function strstrip(str, fnd)
	local i = string.find(str, fnd)
	if i~=nil then
		return strsub(str, 0, i-1)..strsub(str, (i+strlen(fnd)), strlen(str))
	else
		return str
	end
end

function strvars(str, tar)
	local ret = str
	local ins = iif((tar==nil),"",tar)
	
	ret = string.gsub(ret, "%%(target)%%", ins)
	ret = string.gsub(ret, "%%(target)", ins)
	ret = string.gsub(ret, "%%(t)%%", ins)
	ret = string.gsub(ret, "%%(t)", ins)
	
	ret = string.gsub(ret, "%%(%w+)%%", UnitName)
	ret = string.gsub(ret, "%%(%w+)", UnitName)
	
	return ret
end


function strtime(secs)
	local tim = SecondsToTime(secs)
	return strsub(tim, 0, (strlen(tim)-2)) 
end
function strtimeMin(secs)
	local tim = SecondsToTime(secs)
    if string.find(tim, "in") then
		local min = tonumber(strsub(tim,0 , string.find(tim, " ")-1))
		if string.find(tim, "ec") then min = min+1 end
		return iif((min==1),min.." Min",iif((min>1),min.." Mins",""))
	elseif string.find(tim, "ec") then
 		local min = tonumber(strsub(tim,0 , string.find(tim, " ")-1))
		if string.find(tim, "ec") then min = min+1 end
		return iif((min==1),min.." Sec",iif((min>1),min.." Secs",""))
    else
		return "0 Sec";
	end
end
function strtimeM(secs)
	local tim = SecondsToTime(secs)
    if tim==nil then tim = "0s"; end
	if string.find(tim, "in") then
		local min = tonumber(strsub(tim,0 , string.find(tim, " ")-1))
		if string.find(tim, "ec") then min = min+1 end
		return iif((min>=1),min.."m","")
	elseif string.find(tim, "ec") then
		local sec = tonumber(strsub(tim,0 , string.find(tim, " ")-1))
		return iif((sec>=1),sec.."s","")
    else
        return "0s";
	end
end
function WaitSeconds(x)
	tmp = GetTime()
	repeat
		tmp2= GetTime()
	until tmp2-tmp>x
end

function GetSpellID(spellname)
	local i,done,name,id,spellrank=1,false;
	_,_,spellrank = string.find(spellname,"%(("..WORD_RANK.." %d+)%)");
	spellname = string.gsub(spellname,"%("..WORD_RANK.." %d+%)","");
	while not done do
		name,rank = GetSpellName(i,BOOKTYPE_SPELL);
		if not name then
			done=true;
		elseif (name==spellname and not spellrank) or (name==spellname and rank==spellrank) then
			id = i;
		end
		i = i+1;
	end
	return id
end
function GetSpellRank(spellname)
	local i,done,name,ret,spellrank=1,false;
	_,_,spellrank = string.find(spellname,"%(("..WORD_RANK.." %d+)%)");
	spellname = string.gsub(spellname,"%("..WORD_RANK.." %d+%)","");
	while not done do
		name,rank = GetSpellName(i,BOOKTYPE_SPELL);
		if not name then
			done=true;
		elseif (name==spellname and not spellrank) or (name==spellname and rank==spellrank) then
			ret= rank;
		end
		i = i+1;
	end
	return ret
end

function GetContainerTextureByName(item)
	GetContainerItemByName(item)
	local texture, itemCount, locked, quality, readable = GetContainerItemInfo(itemBagNum,itemSlotNum);
	Echo(texture);
end
